﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Pays;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Pays
{
    public class PayRepository : DapperDbContextRepository, IPayRepository
    {
        public PayRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示费用记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        /// <param name="cardnumber"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<PayDto>> GetShowPay(string name, DateTime? time, string cardnumber, int pageindex, int pagesize)
        {
            PageResponse<PayDto> response = new PageResponse<PayDto>();
            var sql = $"select a.*,b.License from Pay a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.PayManager like '%" + name + "%'";
            }
            if (time != null)
            {
                sql += $" and a.UseTime='{time}'";
            }
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            var list = await this.QueryAsync<PayDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<PayDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除费用记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelPay(int id)
        {
            var sql = $"update Pay set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除费用记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsPay(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Pay set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="payDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdatePay(PayDto payDto)
        {
            var sql = $"update Pay set PayTitle='{payDto.PayTitle}',CarNumber='{payDto.CarNumber}',PayTypeId='{payDto.PayTypeId}',PayNum='{payDto.PayNum}',PayDesc='{payDto.PayDesc}',UseTime='{payDto.UseTime.ToString("yyyy/MM/dd")}',Remark='{payDto.Remark}' where Id='{payDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加费用记录
        /// </summary>
        /// <param name="payDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddPay(PayDto payDto)
        {
            var sql = $"insert into Pay values('{payDto.PayNumber}','{payDto.PayTitle}','{payDto.PayTypeId}','{payDto.CarNumber}','{payDto.PayNum}','{payDto.PayManager}','{payDto.PayDesc}','{payDto.UseTime.ToString("yyyy/MM/dd")}','{payDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, payDto);
            return list;
        }
    }
}
